{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e475f3c",
   "metadata": {},
   "source": [
    "## 4.1 基础知识\n",
    "### 4.1.1 分支惯用写法\n",
    "1. if/elif/else\n",
    "    - python 3.10后引入了match/case，类似C语言switch/case\n",
    "2. 省略0值判断\n",
    "    ``` python \n",
    "    if containers_count == 0,\n",
    "    ```\n",
    "    - 只有为0时才满足分支条件\n",
    "    ``` python\n",
    "    if not containers_count:\n",
    "    ```\n",
    "    - 当containers_count的值为0、None、空字符串等时，都可以满足分支条件\n",
    "3. 除整形外，其他内置类型的布尔值如下：\n",
    "    - 布尔值为假： None、0、False、[]、()、{}、set()、frozenset(),等等\n",
    "    - 布尔值为真：非0的数值、True、非空的序列、元组、字典、用于定义的类和实例，等等\n",
    "4. 把否定逻辑移入表达式内\n",
    "    - 使用not表达否定的意义\n",
    "5. 尽可能让三元表达式保持简单\n",
    "    - true_value if <expression> else false_value\n",
    "### 4.1.2 修改对象的布尔值\n",
    "- 魔法方法\n",
    "**__len__、__bool__**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d45f56b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There's some users in collection!\n"
     ]
    }
   ],
   "source": [
    "class UserCollection:\n",
    "    \"\"\"用于保存多个用户的集合工具类\"\"\"\n",
    "    def __init__(self, users):\n",
    "        self.items = users\n",
    "    \n",
    "    def __len__(self):\n",
    "        return len(self.items)\n",
    "users = UserCollection(['piglei', 'raymond'])\n",
    "\n",
    "if users:\n",
    "    print(\"There's some users in collection!\")\n",
    "\n",
    "class ScoreJudger:\n",
    "    \"\"\"仅当分数大于60时为真\"\"\"\n",
    "    def __init__(self, score):\n",
    "        self.score = score\n",
    "    def __bool__(self):\n",
    "        return self.score >= 60"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3a99792",
   "metadata": {},
   "source": [
    "### 4.1.3 与None比较时使用is运算符\n",
    "1. ==对比两个对象的值是否相等，行为可被__eq__方法重载\n",
    "2. is判断两个对象是否是内存里的同一东西，无法被重载\n",
    "3. 当需要判断某个对象是否为None、True、False时用is，其他均用==即可\n",
    "4. 小例外：“整形驻留”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7027f43c",
   "metadata": {},
   "source": [
    "## 4.2 案例故事\n",
    "1. 使用bisect优化范围类分支判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75f63b7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "import bisect\n",
    "breakpoints = [10, 20, 30]\n",
    "print(bisect.bisect(breakpoints, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c79363e6",
   "metadata": {},
   "source": [
    "2. 使用字典优化分支代码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "711f02f4",
   "metadata": {},
   "source": [
    "## 4.3 编程建议\n",
    "### 4.3.1 尽量避免多层分支嵌套\n",
    "- “提前返回技巧”，函数就会变得扁平，“扁平优于嵌套”\n",
    "### 4.3.2 别写太复杂的条件表达式\n",
    "- 如果条件表达复杂，将其封装成函数或者对应的类方法，提高分支的可读性\n",
    "### 4.3.3 尽量降低分支代码的相似性\n",
    "### 4.3.4 使用“德摩根定律”\n",
    "- not A or not B等价于not(A and B)\n",
    "### 4.3.5 使用all()/any()函数构建条件表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d007f5eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def all_numbers_gt_10_2(numbers):\n",
    "    return bool(numbers) and all(n > 10 for n in numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "247edf65",
   "metadata": {},
   "source": [
    "### 4.3.6 留意and和or的运算优先级\n",
    "- and的运算优先级高于or\n",
    "### 4.3.7 避开or运算符的陷阱\n",
    "- “短路求值”"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
